package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper  setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override

    public PageResult getSetMealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),
                    setmealPageQueryDTO.getPageSize());
        Page<SetmealVO>page =setmealMapper.getSetmea(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 套餐新增
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void getpackage(SetmealDTO setmealDTO) {
        //insert
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        setmealDTO.getSetmealDishes().forEach(s -> s.setSetmealId(setmeal.getId()));
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());

    }

    /**
     * 批量删除
     * @param ids
     */

    @Override
    @Transactional
    public void deleteSetmal(List<Long> ids) {
        //判断是否为起状态
        Integer count = setmealMapper.getdelete(ids);
        if (count  >0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        System.out.println("123");
        //删除套餐
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);

    }

    /**
     * 启动 禁用
     * @param status
     * @param id
     */
    @Override
    public void updateSteatus(Integer status, Long id) {
            Setmeal build = Setmeal.builder().status(status).id(id).build();
            setmealMapper.update(build);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
         SetmealVO setmealVO = new SetmealVO();
         Setmeal setmeal=setmealMapper.getSetmealById(id);
         BeanUtils.copyProperties(setmeal,setmealVO);

        List<SetmealDish>list= setmealDishMapper.getListBySetMealId(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO ;
    }

    @Override
    @Transactional
    public void editSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //修改套餐
        setmealMapper.update(setmeal);

        //删除套餐关联的菜品
        setmealDishMapper.deleteBySetMealId(setmealDTO.getId());

        //重新添加套餐关联的菜品
        setmealDTO.getSetmealDishes().forEach(s -> s.setSetmealId(setmealDTO.getId()));
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());
    }
}
